package com.heima.kafka.sample;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.Message;
import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * 生产者
 */
public class ProducerQuickStart {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.kafka的配置信息
        Properties properties = new Properties();
        //kafka的连接地址
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
        //发送失败，失败的重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG,5);
        //消息key的序列化器
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //消息value的序列化器
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //ack配置  消息确认机制
        properties.put(ProducerConfig.ACKS_CONFIG,"all");
        //重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG,10);
        //数据压缩：默认不压缩
        properties.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,"lz4");
        //2.生产者对象
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);
        //封装发送的消息
        Message message = Message.builder().build();

//        ProducerRecord<String,String> record1 = new ProducerRecord<String, String>("hot_article_agg_topic","hello add");
        ProducerRecord<String,String> record2 = new ProducerRecord<String, String>("hot_article_agg_topic",JSON.toJSONString(message));
//        producer.send(record1);
        producer.send(record2);

        //3.发送消息
//        long begin = System.nanoTime();
//        for (int i = 0; i < 100; i++) {
//            RecordMetadata recordMetadata = producer.send(record).get();
//            System.out.println(recordMetadata.offset());
//
//        }

//        long begin1 = System.nanoTime();
//        for (int i = 0; i < 100; i++) {
//            producer.send(record, new Callback() {
//                @Override
//                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
//                    if (e != null){
//                        System.out.println(e);
//                    }
//                }
//            });
//        }
//        long end1  = System.nanoTime();
//        System.out.println(end1-begin1);


        //4.关闭消息通道，必须关闭，否则消息发送不成功
        producer.close();
    }

}